import 'package:flutter/material.dart';
import 'package:flutter_application_testview/fitness_app/models/tabIcon_data.dart';
import 'package:flutter_application_testview/fitness_app/training/training_screen.dart';
import 'package:flutter_application_testview/model/heartRate_data.dart';
import 'package:provider/provider.dart';
import 'bottom_navigation_view/bottom_bar_view.dart';
import 'fitness_app_theme.dart';
import 'my_diary/my_diary_screen.dart';

///我的记录页面 带有底部导航
class FitnessAppHomeScreen extends StatefulWidget {
  @override
  _FitnessAppHomeScreenState createState() => _FitnessAppHomeScreenState();
}

class _FitnessAppHomeScreenState extends State<FitnessAppHomeScreen>
    with TickerProviderStateMixin {
  AnimationController? animationController;

  List<TabIconData> tabIconsList = TabIconData.tabIconsList;

  Widget tabBody = Container(color: FitnessAppTheme.background);
  late HeartRateData heartRateData;
  @override
  void initState() {
    tabIconsList.forEach((TabIconData tab) {
      tab.isSelected = false;
    });
    tabIconsList[0].isSelected = true;

    animationController = AnimationController(
      duration: const Duration(milliseconds: 600),
      vsync: this,
    );
    tabBody = MyDiaryScreen(animationController: animationController);

    
    super.initState();

// 延迟到框架初始化完成后操作
    WidgetsBinding.instance.addPostFrameCallback((_) {
      // 安全获取 Provider 数据
      final heartRateData = Provider.of<HeartRateData>(context, listen: false);
      if (!heartRateData.isRunning) {
        // 添加异步延迟避免同步触发
        Future.microtask(() => heartRateData.start());
        heartRateData.userName="测试用户";
        heartRateData.age=18;
        heartRateData.gender="男";
        heartRateData.height=1.7;
        heartRateData.weight=60;
        heartRateData.devicesName="测试设备";
        heartRateData.devicesMac="00:00:00:00:00:00";
        heartRateData.devicesBattery=100;
        heartRateData.notes="测试备注";
  
        heartRateData.activityType="跑步";

      }
    });

  }

  @override
  void dispose() {
    animationController?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: FitnessAppTheme.background,
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: FutureBuilder<bool>(
          future: getData(),
          builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
            if (!snapshot.hasData) {
              return const SizedBox();
            } else {
              return Stack(children: <Widget>[tabBody, bottomBar()]);
            }
          },
        ),
      ),
    );
  }

  Future<bool> getData() async {
    await Future<dynamic>.delayed(const Duration(milliseconds: 200));
    return true;
  }

  Widget bottomBar() {
    return Column(
      children: <Widget>[
        const Expanded(child: SizedBox()),
        BottomBarView(
          tabIconsList: tabIconsList,
          addClick: () {
          //     // 添加按钮点击逻辑
          // animationController?.reverse().then<dynamic>((data) {
          //   if (!mounted) {
          //     return;
          //   }
          //   setState(() {
          //     tabBody = TrainingScreen(
          //       animationController: animationController,
          //     );
          //   });
          // });
          }, //点击添加按钮
          changeIndex: (int index) {
            //切换底部导航
            if (index == 0) {
              animationController?.reverse().then<dynamic>((data) {
                if (!mounted) {
                  return;
                }
                setState(() {
                    tabBody = MyDiaryScreen(
                    animationController: animationController);
                });
              });
            } else if (index == 1) {
              animationController?.reverse().then<dynamic>((data) {
                if (!mounted) {
                  return;
                }
                setState(() {

                  tabBody = TrainingScreen(
                    animationController: animationController,
                  );
                });
              });
            }
          },
        ),
      ],
    );
  }
}
